package com.anysoft.fund;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.QueryFundListBean;

import java.util.ArrayList;
import java.util.List;

public class CHAdapterHot extends BaseExpandableListAdapter {
	private List<FundInfosGroup> groupList;
	private Context context;
	protected List<CHScrollView_Hot> mHScrollViews;

	/** 0代表默认状态，1代表降序，2代表升序 */
	private int[] group_state_1;
	private int[] group_state_2;

	private boolean isOnlyGroup2;

	private OnFundDetailHotListener fundDetailHotListener;

	public CHAdapterHot(Context context, List<FundInfosGroup> groupList, List<CHScrollView_Hot> mHScrollViews,
						int[] group_state_1, int[] group_state_2, boolean isOnlyGroup2) {
		this.context = context;
		this.groupList = groupList;
		this.mHScrollViews = mHScrollViews;
		this.group_state_1 = group_state_1;
		this.group_state_2 = group_state_2;
		this.isOnlyGroup2 = isOnlyGroup2;
	}

	public void SetOnFundDetailHotListener(OnFundDetailHotListener onFundDetailHotListener) {
		fundDetailHotListener = onFundDetailHotListener;
	}

	public class ViewHolder {
		RelativeLayout rl_title;
		TextView item_icon_new;
		TextView item_icon_tui;
		TextView item_titlev;
		TextView item_titlev_code;
		TextView item_datav1;
		TextView item_datav1_1;
		RelativeLayout rl_datav1;
		TextView item_datav2;
		TextView item_datav3;
		TextView item_datav4;
		TextView item_datav5;
		TextView item_datav6;
		TextView item_datav7;
		TextView item_datav8;
		CHScrollView_Hot item_chscroll_scroll;

		ViewHolder(View view) {
			rl_title = (RelativeLayout) view.findViewById(R.id.rl_title);
			item_icon_new = (TextView) view.findViewById(R.id.item_icon_new);
			item_icon_tui = (TextView) view.findViewById(R.id.item_icon_tui);
			item_titlev = (TextView) view.findViewById(R.id.item_titlev);
			item_titlev_code = (TextView) view.findViewById(R.id.item_titlev_code);
			item_datav1 = (TextView) view.findViewById(R.id.item_datav1);
			item_datav1_1 = (TextView) view.findViewById(R.id.item_datav1_1);
			rl_datav1 = (RelativeLayout) view.findViewById(R.id.rl_datav1);
			item_datav2 = (TextView) view.findViewById(R.id.item_datav2);
			item_datav3 = (TextView) view.findViewById(R.id.item_datav3);
			item_datav4 = (TextView) view.findViewById(R.id.item_datav4);
			item_datav5 = (TextView) view.findViewById(R.id.item_datav5);
			item_datav6 = (TextView) view.findViewById(R.id.item_datav6);
			item_datav7 = (TextView) view.findViewById(R.id.item_datav7);
			item_datav8 = (TextView) view.findViewById(R.id.item_datav8);
			item_chscroll_scroll = (CHScrollView_Hot) view.findViewById(R.id.item_chscroll_scroll);
		}
	}

	public class ViewHolderGroup {
		TextView product_name_txt;
		TextView tv_B_1;
		TextView tv_B_2;
		TextView tv_B_3;
		TextView tv_B_4;
		TextView tv_B_5;
		TextView tv_B_6;
		TextView tv_B_7;
		TextView tv_B_8;
		CHScrollView_Hot item_scroll_title_group;
		RelativeLayout ll_B_1;
		RelativeLayout ll_B_2;
		RelativeLayout ll_B_3;
		RelativeLayout ll_B_4;
		RelativeLayout ll_B_5;
		RelativeLayout ll_B_6;
		RelativeLayout ll_B_7;
		RelativeLayout ll_B_8;
		ImageView img_B_1;
		ImageView img_B_2;
		ImageView img_B_3;
		ImageView img_B_4;
		ImageView img_B_5;
		ImageView img_B_6;
		ImageView img_B_7;
		ImageView img_B_8;

		ViewHolderGroup(View view) {
			item_scroll_title_group = (CHScrollView_Hot) view.findViewById(R.id.item_scroll_title_group);
			product_name_txt = (TextView) view.findViewById(R.id.product_name_txt);
			ll_B_1 = (RelativeLayout) view.findViewById(R.id.ll_B_1);
			ll_B_2 = (RelativeLayout) view.findViewById(R.id.ll_B_2);
			ll_B_3 = (RelativeLayout) view.findViewById(R.id.ll_B_3);
			ll_B_4 = (RelativeLayout) view.findViewById(R.id.ll_B_4);
			ll_B_5 = (RelativeLayout) view.findViewById(R.id.ll_B_5);
			ll_B_6 = (RelativeLayout) view.findViewById(R.id.ll_B_6);
			ll_B_7 = (RelativeLayout) view.findViewById(R.id.ll_B_7);
			ll_B_8 = (RelativeLayout) view.findViewById(R.id.ll_B_8);

			img_B_1 = (ImageView) view.findViewById(R.id.img_B_1);
			img_B_2 = (ImageView) view.findViewById(R.id.img_B_2);
			img_B_3 = (ImageView) view.findViewById(R.id.img_B_3);
			img_B_4 = (ImageView) view.findViewById(R.id.img_B_4);
			img_B_5 = (ImageView) view.findViewById(R.id.img_B_5);
			img_B_6 = (ImageView) view.findViewById(R.id.img_B_6);
			img_B_7 = (ImageView) view.findViewById(R.id.img_B_7);
			img_B_8 = (ImageView) view.findViewById(R.id.img_B_8);

			tv_B_1 = (TextView) view.findViewById(R.id.tv_B_1);
			tv_B_2 = (TextView) view.findViewById(R.id.tv_B_2);
			tv_B_3 = (TextView) view.findViewById(R.id.tv_B_3);
			tv_B_4 = (TextView) view.findViewById(R.id.tv_B_4);
			tv_B_5 = (TextView) view.findViewById(R.id.tv_B_5);
			tv_B_6 = (TextView) view.findViewById(R.id.tv_B_6);
			tv_B_7 = (TextView) view.findViewById(R.id.tv_B_7);
			tv_B_8 = (TextView) view.findViewById(R.id.tv_B_8);
		}
	}

	@Override
	public int getGroupCount() {
		return groupList == null ? 0 : groupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return groupList == null ? 0 : groupList.get(groupPosition).fundInfosBeans.size();
	}

	@Override
	public FundInfosGroup getGroup(int groupPosition) {
		return groupList == null ? null : groupList.get(groupPosition);
	}

	@Override
	public QueryFundListBean.DataBean getChild(int groupPosition, int childPosition) {
		return groupList == null ? null : (groupList.get(groupPosition)).fundInfosBeans.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	public interface GoBackMethodListener_Hot {
		public void callBack(int group, int clickPosition);
	}

	private GoBackMethodListener_Hot _BackMethodListener_Hot;

	public void setGoBackMethodListener(GoBackMethodListener_Hot _BackMethodListener_Hot) {
		this._BackMethodListener_Hot = _BackMethodListener_Hot;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ViewHolderGroup holder;

		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.list_item_group_hot, null);
			holder = new ViewHolderGroup(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolderGroup) convertView.getTag();
		}

		mHScrollViews.add(holder.item_scroll_title_group);

		FundInfosGroup groupObj = getGroup(groupPosition);

		holder.ll_B_1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 0);
				}
			}
		});

		holder.ll_B_2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 1);
				}
			}
		});

		holder.ll_B_3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 2);
				}
			}
		});

		holder.ll_B_4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 3);
				}
			}
		});

		holder.ll_B_5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 4);
				}
			}
		});

		holder.ll_B_6.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 5);
				}
			}
		});

		holder.ll_B_7.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 6);
				}
			}
		});

		holder.ll_B_8.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (_BackMethodListener_Hot != null) {
					_BackMethodListener_Hot.callBack(groupPosition, 7);
				}
			}
		});
		if (groupObj != null) {
			holder.product_name_txt.setText("基金名称");
		}
		ArrayList<ImageView> img_list = new ArrayList<ImageView>();
		img_list.add(holder.img_B_1);
		img_list.add(holder.img_B_2);
		img_list.add(holder.img_B_3);
		img_list.add(holder.img_B_4);
		img_list.add(holder.img_B_5);
		img_list.add(holder.img_B_6);
		img_list.add(holder.img_B_7);
		img_list.add(holder.img_B_8);

		holder.tv_B_3.setText("近1周");
		holder.tv_B_4.setText("近1月");
		holder.tv_B_5.setText("近3月");
		holder.tv_B_6.setText("近6月");
		holder.tv_B_7.setText("今年以来");
		holder.tv_B_8.setText("成立以来");
		switch (groupPosition) {
		case 0:
			holder.tv_B_1.setText("最新净值");
			holder.tv_B_2.setText("日涨幅");

			for (int i = 0; i < group_state_1.length; i++) {
				switch (group_state_1[i]) {
				case 0:
					img_list.get(i).setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_angle));
					break;
				case 1:
					img_list.get(i).setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rank_down));
					break;
				case 2:
					img_list.get(i).setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rank_up));
					break;
				default:
					break;
				}
			}
			break;
		case 1:
			holder.tv_B_1.setText("万份收益");
			holder.tv_B_2.setText("七日年化");

			for (int i = 0; i < group_state_2.length; i++) {
				switch (group_state_2[i]) {
				case 0:
					img_list.get(i).setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_angle));
					break;
				case 1:
					img_list.get(i).setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rank_down));
					break;
				case 2:
					img_list.get(i).setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_rank_up));
					break;
				default:
					break;
				}
			}
			break;

		default:
			break;
		}
		return convertView;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
							 ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.list_item_hot, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		mHScrollViews.add(holder.item_chscroll_scroll);
		final QueryFundListBean.DataBean dataItem = getChild(groupPosition, childPosition);
		if (Integer.parseInt(dataItem.getIsNewFund()) == 1) {
			holder.item_icon_new.setVisibility(View.VISIBLE);
		} else {
			holder.item_icon_new.setVisibility(View.GONE);
		}
		if (Integer.parseInt(dataItem.getIsRecommend()) == 1) {
			holder.item_icon_tui.setVisibility(View.VISIBLE);
		} else {
			holder.item_icon_tui.setVisibility(View.GONE);
		}
		// if (Integer.parseInt(dataItem.getIsRecommend()) == 1) {
		// holder.item_icon.setVisibility(View.VISIBLE);
		// holder.item_icon.setImageDrawable(context.getResources().getDrawable(R.drawable.market_sort_down));
		// } else {
		// holder.item_icon.setVisibility(View.GONE);
		// }
		holder.item_titlev.setText(dataItem.getFundname());
		holder.item_titlev_code.setText(dataItem.getFundcode());

		if (isOnlyGroup2) {
			holder.item_datav1.setText(dataItem.getHfincomeratio());
			// initTextview(holder.item_datav1, dataItem.getHfincomeratio());
			initTextview(holder.item_datav2, dataItem.getIncomeratio());
		} else {
			switch (groupPosition) {
			case 0:
				holder.item_datav1.setText(dataItem.getPernetvalue());
				// initTextview(holder.item_datav1, dataItem.getPernetvalue());
				initTextview(holder.item_datav2, dataItem.getDayinc());
				break;
			case 1:
				holder.item_datav1.setText(dataItem.getHfincomeratio());
				// initTextview(holder.item_datav1,
				// dataItem.getHfincomeratio());
				initTextview(holder.item_datav2, dataItem.getIncomeratio());
				break;

			default:
				break;
			}
		}

		String date = dataItem.getNavdate();
		if (date.length() == 8) {
			date = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
		}
		holder.item_datav1_1.setText(date);

		initTextview(holder.item_datav3, dataItem.getPeriodWeek());
		initTextview(holder.item_datav4, dataItem.getPeriodMonthOne());
		initTextview(holder.item_datav5, dataItem.getPeriodMonthThree());
		initTextview(holder.item_datav6, dataItem.getPeriodMonthSix());
		initTextview(holder.item_datav7, dataItem.getPeriodSinceYear());
		initTextview(holder.item_datav8, dataItem.getPeriodSinceEstablish());
		// holder.item_datav2.setText(dataItem.getDayinc());
		// holder.item_datav3.setText(dataItem.getPeriodWeek());
		// holder.item_datav4.setText(dataItem.getPeriodMonthOne());
		// holder.item_datav5.setText(dataItem.getPeriodMonthThree());
		// holder.item_datav6.setText(dataItem.getPeriodMonthSix());
		// holder.item_datav7.setText(dataItem.getPeriodSinceYear());
		// holder.item_datav8.setText(dataItem.getPeriodSinceEstablish());

		ArrayList<TextView> textViews = new ArrayList<TextView>();
		textViews.add(holder.item_datav2);
		textViews.add(holder.item_datav3);
		textViews.add(holder.item_datav4);
		textViews.add(holder.item_datav5);
		textViews.add(holder.item_datav6);
		textViews.add(holder.item_datav7);
		textViews.add(holder.item_datav8);

		holder.rl_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fundDetailHotListener != null) {
					fundDetailHotListener.OnDetailClick(dataItem);
				}
			}
		});

		holder.rl_datav1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fundDetailHotListener != null) {
					fundDetailHotListener.OnDetailClick(dataItem);
				}
			}
		});

		for (TextView textView : textViews) {
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					if (fundDetailHotListener != null) {
						fundDetailHotListener.OnDetailClick(dataItem);
					}
				}
			});
		}
		return convertView;
	}

	private void initTextview(TextView textView, String str) {
		if (TextUtils.isEmpty(str)) {
			textView.setText("--");
		} else {
			if (str.contains("-")) {
				textView.setTextColor(context.getResources().getColor(R.color.black));
				textView.setText(str);
			} else {
				textView.setTextColor(context.getResources().getColor(R.color.red));
				textView.setText(str);
			}
		}
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	/**
	 * 详情单击监听器
	 */
	public interface OnFundDetailHotListener {

		/**
		 * @param fundInfosBean
		 */
		void OnDetailClick(QueryFundListBean.DataBean fundInfosBean);
	}

}
